//146.LRU缓存 - (类rides)
//https://leetcode.cn/problems/lru-cache/?envType=study-plan-v2&envId=top-100-liked
class LRUCache {
public:
    LRUCache(int capacity):cap(capacity) {
    }
    
    int get(int key) {
        if(hash.count(key)) 
        {
            int value = hash[key]->second;
            l.erase(hash[key]);
            hash.erase(key);
            put(key,value);
            return value;
        }
        return -1;
    }
    
    void put(int key, int value) {
        if(hash.count(key)) 
        {
            l.erase(hash[key]);
            hash.erase(key);
            put(key,value);
        }
        else
        {
            if(l.size() == cap)
            {
                hash.erase(l.back().first);
                l.pop_back();
            }
            l.push_front({key,value});
            hash[key] = l.begin();
        }
    }
private:
    int cap;
    list<pair<int,int>> l;
    unordered_map<int,list<pair<int,int>>::iterator> hash;
};